package com.example.controller;

import com.example.entity.po.Primary;
import com.example.utils.ResultUtils;
import com.example.service.PrimaryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Controller
@RestController
public class PrimaryController {

    @Autowired
    private PrimaryService primaryService;

    //展示合作小学
    @GetMapping("/primaryList")
    @ResponseBody
    public ResultUtils primaryList(){
        List<Primary> list=primaryService.queryPrimary();
        if (list.size()==0){
            return ResultUtils.Failure("查询学校信息失败",null);
        }else {
            return ResultUtils.Successful("查询学校信息成功",list);
        }
    }

    //添加合作小学
    @PostMapping("/addPrimarySchool")
    @ResponseBody
    public ResultUtils addPrimarySchool(@RequestBody Primary primary){
        int res= primaryService.addPrimarySchool(primary);
        if (res==0){
            return ResultUtils.Failure("添加学校失败",null);
        }else {
            return ResultUtils.Successful("添加学校成功",null);
        }
    }

    //删除合作小学信息
    @PostMapping("/deletePrimary")
    @ResponseBody
    public ResultUtils deletePrimary(int id){
        int res= primaryService.deletePrimary(id);
        if (res==0){
            return ResultUtils.Failure("删除学校失败",null);
        }else {
            return ResultUtils.Successful("删除学校成功",null);
        }
    }

    //修改合作小学信息
    @PostMapping("/updatePrimary")
    @ResponseBody
    public ResultUtils updatePrimary(@RequestBody Primary primary){
        int res= primaryService.updatePrimary(primary);
        if (res==0){
            return ResultUtils.Failure("修改学校信息失败",null);
        }else {
            return ResultUtils.Successful("修改学校信息成功",null);
        }
    }

    //对合作小学进行模糊查询
    @PostMapping("/fuzzyPrimary")
    public ResultUtils fuzzyPrimary(String schoolName){
        List<Primary> primaryList= primaryService.fuzzyPrimary(schoolName);
        if (primaryList.size()==0){
            return ResultUtils.Failure("搜索合作小学失败或无该合作小学",null);
        }else {
            return ResultUtils.Successful("搜索合作小学成功",primaryList);
        }
    }

    //添加教学点
    @PostMapping("/addPlace")
    public ResultUtils addPlace(@RequestBody Primary primary){
        int res= primaryService.addPlace(primary);
        if (res==0){
            return ResultUtils.Failure("添加教学点失败",null);
        }else {
            return ResultUtils.Successful("添加教学点成功",null);
        }
    }

    //删除教学点
    @PostMapping("/deletePlace")
    public ResultUtils deletePlace(int id){
        int res= primaryService.deletePlace(id);
        if (res==0){
            return ResultUtils.Failure("删除教学点失败",null);
        }else {
            return ResultUtils.Successful("删除教学点成功",null);
        }
    }

    //查询教学点
    @GetMapping("/PlaceList")
    public ResultUtils queryPlace(){
        List<Primary> list=primaryService.queryPlace();
        if (list.size()==0){
            return ResultUtils.Failure("查询教学点信息失败",null);
        }else {
            return ResultUtils.Successful("查询教学点信息成功",list);
        }
    }

    //修改教学点
    @PostMapping("/updatePlace")
    public ResultUtils updatePlace(@RequestBody Primary primary){
        int res= primaryService.updatePlace(primary);
        if (res==0){
            return ResultUtils.Failure("修改教学点信息失败",null);
        }else {
            return ResultUtils.Successful("修改教学点信息成功",null);
        }
    }

    //对教学点进行模糊查询
    @PostMapping("/fuzzyPlace")
    public ResultUtils fuzzyPlace(String schoolName){
        List<Primary> primaryList= primaryService.fuzzyPlace(schoolName);
        if (primaryList.size()==0){
            return ResultUtils.Failure("搜索教学点成功或无该教学点",null);
        }else {
            return ResultUtils.Successful("搜索教学点学成功",primaryList);
        }
    }




}
